package com.lvhx.springboot.springbatch.sharedata;

import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.ExecutionContext;
import org.springframework.batch.repeat.RepeatStatus;

/**
 * @auther 吕宏旭
 * @date 2019/9/5 15:05
 *
 * 方式一：
 * 通过executionContext实现step数据共享
 *
 */
public class GetDataTasklet implements Tasklet {

    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    @Override
    public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {

        ExecutionContext executionContext = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();

        String username = executionContext.getString("username");
        System.out.println("username = "+username);

        return RepeatStatus.FINISHED;
    }
}
